﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MrPoetic.MatLab.Algebra
{
    

    public class LambdaUtil
    {
        /* [质数检测]器 - 基准 */
        public static Func<int, bool> isPrime = number =>
        {
            for (int i = 2; i <= Math.Sqrt(number); i++)
                if (number % i == 0)
                    return false;
            return true;
        };
        /* [2方数]检测器 - 上层算法 */
        public static Func<long, bool> is2Times = Num =>
        {
            // 注意 bit位操作符的优先级非常低 一定一定一定要加括号
            return (Num & (Num - 1)) == 0;
        };
        /* [完全平方数]检测器 */
        public static Func<int, bool> isSqrine = Num =>
        {
            return Math.Sqrt(Num) == Math.Floor(Math.Sqrt(Num));
        };
        
        // CRC84 - 循环校检数字位数
        public static Func<long, int> bitWid = Num =>
        {
            Int32 i = 0;
            while (Num > 0)
            {
                Num /= 10;
                i++;
            }
            return i;
        };

        /* [黄金分割算法 - by.dimchumhaul] */
        public static Func<Int32, Int32> GoldenSlice = upper =>
        {
            if (upper <= 20) return upper;
            else if (upper >= 21 && upper <= 36)
            {
                Int32 N = (Int32)(upper * 0.618);
                return N >>= 1;
            }
            else return GoldenSlice((Int32)Math.Sqrt(upper));
        };

        /* 查看一个十进制数组的二进制表达 使用8Bit电荷位 */
        public static Action<Int32> Show8Bit = int32Val =>
        {
            Console.WriteLine("数字[{0}]的二进制位表达.[{1}]", int32Val,
                Convert.ToString(int32Val, 2));
        };
    }
}
